package com.young.shop.api.service.impl;

import com.young.shop.api.dao.CartsMapper;
import com.young.shop.api.dao.GoodsDaoMapper;
import com.young.shop.api.service.CartsService;
import com.young.shop.api.utils.CartException;
import com.young.shop.pojo.entity.Carts;
import com.young.shop.pojo.entity.Goods;
import com.young.shop.pojo.qo.CartQO;
import com.young.shop.pojo.qo.UpdatePo;
import com.young.shop.pojo.vo.StatusCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
// * @author LY
// * @date 2021/1/11 9:33
// */

@Service
public class CartsServiceImpl implements CartsService {
    @Resource
    CartsMapper cartsMapper;

    @Resource
    GoodsDaoMapper goodsDaoMapper;

    /**
     * 添加购物车
     * (根据商品id判断和库存是否发生改变才可添加购物车)
     *
     * @param cartQO
     */
    @Override
    public List<Carts> AddCarts(CartQO cartQO) {
        Carts cart = new Carts();
        List<Carts> carts = null;
        int i =0;
        //查询购物车是否存在相应商品,无直接添加,有的话添加数量
        Carts shopCart = cartsMapper.selectCommodity(cartQO.getCommodityId());
        if (shopCart !=null){
            //通过数量改变 重新计算金额
            //根据id查询商品单价
            Goods goods = goodsDaoMapper.selectGoodsById(String.valueOf(cartQO.getCommodityId()));
            BigDecimal big = new BigDecimal(0);
            BigDecimal bigDecimal = new BigDecimal(String.valueOf(goods.getPrice()));
            BigDecimal multiply = bigDecimal.multiply(new BigDecimal(cartQO.getCartNum()));
            big = big.add(multiply);

            //修改参数封装
            UpdatePo updatePo = new UpdatePo();
            updatePo.setProductAmount(big);
            updatePo.setProductId(cartQO.getCommodityId());
            updatePo.setProductNum(cartQO.getCartNum());
            //修改数量总价
            i = cartsMapper.updateNum(updatePo);
        } else {
            //增加购物车参数
            cart.setUserId(cartQO.getUserId());
            cart.setCommodityId(cartQO.getCommodityId());
            cart.setCartNum(cartQO.getCartNum());
            //计算购物车商品总金额
            Goods goods = goodsDaoMapper.selectGoodsById(String.valueOf(cartQO.getCommodityId()));
            BigDecimal big = new BigDecimal(0);
            BigDecimal bigDecimal = new BigDecimal(String.valueOf((goods.getPrice())));
            BigDecimal multiply = bigDecimal.multiply(new BigDecimal(cartQO.getCartNum()));
            big = big.add(multiply);
            cart.setCommodityTotalPrice(big);
            //为空直接添加
            i = cartsMapper.addCartById(cart);
        }

        if (i>0){
            carts = cartsMapper.selectUpdateCart(cartQO.getCommodityId());

        } else {
            throw new CartException(StatusCode.ERROR.getStatus());
        }
        return carts;
    }

    /**
     * 删除购物车
     * @param commodityId
     * @return
     */
    @Override
    public List<Carts> deleteCommodity(long commodityId) {
        List<Carts> carts =null;
        //删除购物车商品
        int i = cartsMapper.deleteById(commodityId);
        if (i>0){
            carts =cartsMapper.selectNewCart();
        }else{
            throw new CartException(StatusCode.ERROR.getStatus());
        }
        return carts;
    }
}
